<?php

namespace app\admin\controller;

use app\admin\BaseController;
use app\admin\model\Dictionary;
use app\admin\model\DictionaryZh;
use app\mobile\model\UserDictionary;
use app\Request;
use think\db\Query;
use think\facade\Validate;


/* 
 * 字典管理
 */
class DictionaryController extends BaseController
{


    /**字典列表
     * @fun:
     *
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author:崔彦雷
     * @time:2022/10/31 下午 2:46
     */
    public function dictionaryList()
    {
        $all = input('param.');

        $flist = Dictionary::where('parent_id',0)
            //->where('is_delete',2)
            ->order('sort asc')
            ->select();
        foreach ($flist as $key => $val){
            $parent_id = $val['id'];
            $clist = Dictionary::where('parent_id',$parent_id)
                //->where('is_delete',2)
                ->order('sort asc')
                ->select()->toArray();

            $flist[$key]['children'] = $clist;
        }
        return $this->suc_json('',$flist);
    }

    /**删除字典
     * @fun:
     *
     * @return \think\response\Json
     * @author:崔彦雷
     * @time:2022/10/31 下午 2:45
     */
    public function delDictionary(Request $request)
    {

        $id = $request->param('id');

        if (empty($id)){
            return $this->err_json('id不能为空','default');
        }

        $dic = Dictionary::where('id', $id)->find();
        $dic->is_delete = 1;
        $dic->update_time = date('Y-m-d H:i:s',time());
        $info = $dic->save();

        if ($info == 1){
            return $this->suc_json('删除成功');
        }else{
            return $this->err_json('删除失败','default');
        }

    }


    /**编辑字典
     * @fun:
     *
     * @return \think\response\Json
     * @author:崔彦雷
     * @time:2022/10/31 下午 2:45
     */
    public function editDictionary(Request $request)
    {
        $param = $request->param();
        $validate = Validate::rule([
            'id|id' => 'require|number',
            'parent_id|父id' => 'require',
            'name|类目名称' => 'require',
            'sort|排序' => 'require',
            'type|类型' => 'require',
        ]);
        if (!$validate->check($param)){
            return $this->err_json($validate->getError());
        }

        $dic = Dictionary::where('id',$param['id'])->find();
        $dic->parent_id = $param['parent_id'];
        $dic->name = $param['name'];
        $dic->sort = $param['sort'];
        $dic->type = $param['type'];
        $info = $dic->save();

        if($info == 1){
            return $this->suc_json('编辑成功');
        }  else {
            return $this->err_json('编辑失败','default');
        }
    }


    /**添加字典
     * @fun:
     *
     * @return \think\response\Json
     * @author:崔彦雷
     * @time:2022/10/31 下午 2:44
     */
    public function addDictionary(Request $request)
    {
        $param = $request->param();
        $validate = Validate::rule([
            'parent_id|父id' => 'require',
            'name|类目名称' => 'require',
            'sort|排序' => 'require',
            'type|类型' => 'require',
        ]);
        if (!$validate->check($param)){
            return $this->err_json($validate->getError());
        }

        $data['name'] = $param['name'];
        $data['parent_id'] = $param['parent_id'];
        $data['sort'] = $param['sort'];
        $data['type'] = $param['type'];

        $add = Dictionary::insertGetId($data);

        if($add){
            return $this->suc_json('添加成功');
        }  else {
            return $this->err_json('添加失败', 'default');
        }
    }
















}